package com.shell.governor.organization.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.entity.auth.AuthOrgan;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.organization.po.PubOrgan;
import com.shell.governor.organization.po.PubOrganType;
import com.shell.governor.organization.service.PubOrganService;
import com.shell.governor.organization.service.PubOrganTypeService;
import com.shell.governor.organization.vo.PubOrganVo;

@RestController
public class PubOrganController extends BaseController {
	@Autowired
	private PubOrganService pubOrganService;
	@Autowired
	private PubOrganTypeService pubOrganTypeService;

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg saveOrganRecord(PubOrganVo pubOrganVo) {
		pubOrganVo.setSourceCode("CUSTOM");
		if (this.pubOrganService.saveOrUpdateOrganRecord(pubOrganVo) != 1) {
			return initRetMsg("success", "������������������");
		}
		return initRetMsg("success", "������������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg updateOrganRecord(PubOrganVo pubOrganVo) {
		pubOrganVo.setSourceCode("CUSTOM");
		if (this.pubOrganService.saveOrUpdateOrganRecord(pubOrganVo) != 1) {
			return initRetMsg("success", "������������������");
		}
		return initRetMsg("success", "������������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������", description = "��������������������������������")
	public List<PubOrganVo> selectOrganTree(PubOrganVo pubOrganVo) {
		try {
			if ("1".equals(pubOrganVo.getDataType())) {
				pubOrganVo.setStruLevel(Integer.valueOf(0));
			} else {
				AuthOrgan curEnt = CommonDataUtil.getCurEnt();
				if (curEnt != null) {
					pubOrganVo.setStruLevel(curEnt.getStruLevel());
					pubOrganVo.setStruPath(curEnt.getStruPath());
				}
			}
			return this.pubOrganService.queryOrganTree(pubOrganVo);
		} catch (Exception e) {
			this.logger.debug("", e);
		}
		return null;
	}

	@RequestMapping
	@Logging(module = "������������", methods = "��������������", description = "������������")
	public String selectAllOrganTree(PubOrganVo pubOrganVo) throws Exception {
		pubOrganVo.setStruLevel(Integer.valueOf(0));
		List<PubOrganVo> organList = this.pubOrganService.queryOrganTree(pubOrganVo);
		String listStr = JsonUtil.toJson(organList, JsonInclude.Include.NON_EMPTY);
		return listStr;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����Id��������")
	public PubOrganVo selectOrganById(String organId) {
		return this.pubOrganService.selectOrganById(organId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg updateOrganStateWithCascade(String organId, String parentId, String organState) {
		String msg = organState.equals("U") ? "����" : organState.equals("D") ? "����" : "����";
		if (this.pubOrganService.updateOrganStateWithCascade(organId, parentId, organState) > 0) {
			return initRetMsg("success", msg + "����������");
		}
		return initRetMsg("error", msg + "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg checkCode(PubOrgan pubOrgan) {
		return initRetMsg("success", false, Boolean.valueOf(this.pubOrganService.checkCodeUnique(pubOrgan)));
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg checkOrganName(PubOrgan pubOrgan) {
		return initRetMsg("success", false, Boolean.valueOf(this.pubOrganService.checkOrganNameUnique(pubOrgan)));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public List<PubOrgan> selectByOrganName(String organName) {
		PubOrgan organ = new PubOrgan();
		organ.setOrganName(organName);
		return this.pubOrganService.selectOrgan(organ);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������������������")
	public PubOrgan selectByOrganId(String organId) {
		PubOrgan organ = new PubOrgan();
		organ.setOrganId(organId);
		return (PubOrgan) this.pubOrganService.selectOrgan(organ).get(0);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������")
	public List<PubOrganType> selectAllType(String organTypes) {
		return this.pubOrganTypeService.getOrganType(organTypes);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������")
	public PubOrganType selectByOrganType(String organType) {
		PubOrganType pot = new PubOrganType();
		pot.setOrganType(organType);
		return (PubOrganType) this.pubOrganTypeService.selectOne(pot);
	}
}
